package com.wz.project.api.sap.controller;

import com.wz.framework.web.domain.AjaxResult;
import com.wz.project.barcodeMsg.domain.BarcodeMsg;
import com.wz.project.barcodeMsg.service.IBarcodeMsgService;
import com.wz.project.sap.DN_POST_DN.domain.ZflWmsDnPostDn;
import com.wz.project.sap.DN_POST_DN.service.IZflWmsDnPostDnService;
import com.wz.project.sap.DN_POST_SERIALNO.domain.ZflWmsDnPostSerialno;
import com.wz.project.sap.DN_POST_SERIALNO.service.IZflWmsDnPostSerialnoService;
import com.wz.project.sap.POST_TMDATA_TMINTERF.domain.YppRfcPostTmdataTminterf;
import com.wz.project.sap.POST_TMDATA_TMINTERF.service.IYppRfcPostTmdataTminterfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/timer")
public class TimerController {

    @Autowired
    SapController sapController;
    @Autowired
    IYppRfcPostTmdataTminterfService yppRfcPostTmdataTminterfService;
    @Autowired
    IZflWmsDnPostSerialnoService zflWmsDnPostSerialnoService;

    @Autowired
    IZflWmsDnPostDnService zflWmsDnPostDnService;
    @Autowired
    IBarcodeMsgService barcodeMsgService;

    //获取主物料数据
    //每天1点执行一次
    @Scheduled(cron = "0 0 7 * * ?")
    @RequestMapping("/getProduct")
    @ResponseBody
    public AjaxResult getProduct() {
        String MATNR="";
        String date =  new SimpleDateFormat("yyyyMMdd").format(new Date());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        Calendar calendar= Calendar.getInstance();
        calendar.setTime(new Date());
//拿到前一天
        calendar.add(Calendar.DAY_OF_MONTH,-1);

        String begingDate = simpleDateFormat.format(calendar.getTime());
        String endDate = date;

        AjaxResult ajaxResult = sapController.getMaterialMasterData("2240", MATNR, begingDate, endDate);

        return ajaxResult;

    }
    @RequestMapping("/getProductOne")
    @ResponseBody
    public AjaxResult getProduct1(String MATNR) {
        AjaxResult ajaxResult = sapController.getMaterialMasterData("2240", MATNR, "", "");

        return ajaxResult;

    }

    //获取内外向交货单
    //每五分钟执行一次
    @Scheduled(cron = "0 */5 * * * ?")
    @RequestMapping("/getInOrOutOrder")
    @ResponseBody
    public AjaxResult getInOrOutOrder() {
        String date =  new SimpleDateFormat("yyyyMMdd").format(new Date());

        String begingDate = date;
        String endDate = date;
        AjaxResult ajaxResult = sapController.getDNData("", begingDate, endDate);
        return ajaxResult;
    }
    @RequestMapping("/getInOrOutOrderOne")
    @ResponseBody
    public AjaxResult getInOrOutOrderOne(String VBELN) {
        AjaxResult ajaxResult = sapController.getDNData(VBELN, "", "");
        return ajaxResult;
    }

    //内外向交货单过账（待确认）
    @Scheduled(cron = "0 */1 * * * ?")
    @RequestMapping("/commitInOrOutOrder")
    @ResponseBody
    public void commitInOrOutOrder() {

        ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();
        zflWmsDnPostDn.setStatus("0");
        List<ZflWmsDnPostDn> zflWmsDnPostDns = zflWmsDnPostDnService.selectDistinctVBELN(zflWmsDnPostDn);
        if(zflWmsDnPostDns.size()!=0) {
            for(ZflWmsDnPostDn zflWmsDnPostDn1:zflWmsDnPostDns){
                zflWmsDnPostDn1.setStatus("0");
                List<ZflWmsDnPostDn> zflWmsDnPostDnList = zflWmsDnPostDnService.selectZflWmsDnPostDnList(zflWmsDnPostDn1);
                sapController.getDNPostData(zflWmsDnPostDnList);
            }

        }
    }
    //质检转非限制
    @Scheduled(cron = "0 */1 * * * ?")
    @RequestMapping("/checkQuanlity")
    @ResponseBody
    public void checkQuanlity() {
        ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();
        zflWmsDnPostDn.setStatus("3");
        List<ZflWmsDnPostDn> zflWmsDnPostDns = zflWmsDnPostDnService.selectDistinctVBELN(zflWmsDnPostDn);
        if(zflWmsDnPostDns.size()!=0) {
            for(ZflWmsDnPostDn zflWmsDnPostDn1:zflWmsDnPostDns){
                sapController.getCheckData(zflWmsDnPostDn1);
            }


        }
    }
    //生产订单
    @Scheduled(cron = "0 */5 * * * ?")
    @RequestMapping("/getProduceOrder")
    @ResponseBody
    public void getProduceOrder() {
        sapController.getProData("", "", "", "", "2240", "", "", "", "");
    }

    @RequestMapping("/getProduceOrderOne")
    @ResponseBody
    public AjaxResult getProduceOrderOne(String AUFNR) {
      return sapController.getProData(AUFNR, "", "", "", "", "", "", "", "");
    }

    //生产订单入库
    @Scheduled(cron = "0 */1 * * * ?")
    @RequestMapping("/commitProduceOrder")
    @ResponseBody
    public void commitProduceOrder() {

        YppRfcPostTmdataTminterf yppRfcPostTmdataTminterf1 = new YppRfcPostTmdataTminterf();
        yppRfcPostTmdataTminterf1.setStatus("0");
        List<YppRfcPostTmdataTminterf> yppRfcPostTmdataTminterfs = yppRfcPostTmdataTminterfService.selectYppRfcPostTmdataTminterfList(yppRfcPostTmdataTminterf1);
        if(yppRfcPostTmdataTminterfs.size()!=0){
            for(YppRfcPostTmdataTminterf yppRfcPostTmdataTminterf:yppRfcPostTmdataTminterfs){
                sapController.getTMDATAData(yppRfcPostTmdataTminterf);
            }
        }
    }
}
